﻿// <kernel for logging mobile worker.>
//     Copyright (C) 2013  APKO
// 
//     This program is free software: you can redistribute it and/or modify
//     it under the terms of the GNU General Public License as published by
//     the Free Software Foundation, either version 3 of the License, or
//     (at your option) any later version.
// 
//     This program is distributed in the hope that it will be useful,
//     but WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//     GNU General Public License for more details.
// 
//     You should have received a copy of the GNU General Public License
//     along with this program.  If not, see <http://www.gnu.org/licenses/>.

using System;
using System.ComponentModel;
using System.Threading;
using System.Windows;
using System.Windows.Threading;
using kernel.gui;

namespace kernel {
    /// <summary>
    ///     Логика взаимодействия для EnterShell.xaml
    /// </summary>
    public partial class EnterShell {
        protected BackgroundWorker showOrder = new BackgroundWorker();
        protected BackgroundWorker showmain = new BackgroundWorker();

        public EnterShell() {
            this.InitializeComponent();
        }

        private void UserControl_Initialized_1(object sender, EventArgs e) {
            this.Mainpage.AdoptedModue.AddNewOrder.Click += this.AddNewOrder_Click;
            this.Orderpage.Cancel.Click += this.Cancel_Click;
            this.showmain.DoWork += this.ShowMainPage;
            this.showmain.RunWorkerCompleted += this.UpdateCompleted;
            this.showOrder.DoWork += this.showOrder_DoWork;
            this.showOrder.RunWorkerCompleted += this.UpdateCompleted;
        }

        private void showOrder_DoWork(object sender, DoWorkEventArgs e) {
            Action<bool> workMethod = this.WorkMethod;
            this.Dispatcher.BeginInvoke(DispatcherPriority.Background,
                                        workMethod, false);
        }

        private void WorkMethod(bool b) {
            Thread.Sleep(5000);
            if (b) {
                Controller.ChangeElement(this.Orderpage, false, false);
                Controller.ChangeElement(this.Mainpage, true);
            }
            else {
                Controller.ChangeElement(this.Mainpage, false, false);
                Controller.ChangeElement(this.Orderpage, true);
            }
        }

        private void Cancel_Click(object sender, RoutedEventArgs e) {
            this.Bysistatus.IsBusy = true;
            this.showmain.RunWorkerAsync();
        }

        private void UpdateCompleted(object sender, RunWorkerCompletedEventArgs e) {
            this.Bysistatus.IsBusy = false;
        }

        private void ShowMainPage(object sender, DoWorkEventArgs doWorkEventArgs) {
            Action<bool> workMethod = this.WorkMethod;
            this.Dispatcher.BeginInvoke(DispatcherPriority.Background,
                                        workMethod, true);
        }

        private void AddNewOrder_Click(object sender, RoutedEventArgs e) {
            this.Bysistatus.IsBusy = true;
            this.showOrder.RunWorkerAsync();
        }
    }
}